package com.itheima.demoset;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class HashSetDemo2 {

    public static void main(String[] args) {
        Student s1 = new Student("张三", 18, 100, "北京");
        Student s6 = new Student("张三", 18, 100, "北京");
        Student s7 = new Student("张三", 18, 100, "北京");
        Student s2 = new Student("李四", 19, 90, "上海");
        Student s3 = new Student("王五", 20, 80, "广州");
        Student s4 = new Student("赵六", 21, 70, "深圳");
        Student s5 = new Student("田七", 22, 60, "杭州");
        Set<Student> set = new HashSet<>();
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        set.add(s5);
        set.add(s6);
        set.add(s7);
        for (Student s : set){
            System.out.println(s);
        }
    }
}

class Student{
    private String name;
    private int age;
    private int score;
    private String address;
    public Student(String name, int age, int score, String address) {
        this.name = name;
        this.age = age;
        this.score = score;
        this.address = address;
    }
    public Student() {
    }

    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", score=" + score + ", address=" + address + "]" + "\n";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public boolean equals(Object o) {
        // 自己和自己比较
        if (this == o) return true;
        // 如果o为空或者o的类型和当前对象的类型不一致，则返回false
        if (o == null || this.getClass() != o.getClass()) return false;
        // 强制类型转换
        Student student = (Student) o;
        // 比较属性
        return age == student.age && score == student.score && Objects.equals(name, student.name) && Objects.equals(address, student.address);
    }

    @Override
    public int hashCode() {
        // 如果后四个内容参数相同，则返回相同的hashCode
        return Objects.hash(name, age, score, address);
    }
}